package com.hdoit.ares.web.policy.apis;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.cust.service.ICustomerService;
import com.hdoit.ares.logical.insu.model.FanhuaCar;
import com.hdoit.ares.logical.insu.model.FanhuaCarBusinessRiskKinds;
import com.hdoit.ares.logical.insu.model.ZhonganCar;
import com.hdoit.ares.logical.insu.service.IFanhuaAccidentService;
import com.hdoit.ares.logical.insu.service.IFanhuaCarBusinessRiskKindsService;
import com.hdoit.ares.logical.insu.service.IFanhuaCarService;
import com.hdoit.ares.logical.insu.service.IZhonganCarService;
import com.hdoit.ares.logical.policy.model.ListPolicy;
import com.hdoit.ares.logical.policy.service.IListPolicyService;
import com.hdoit.ares.selfcommon.utils.PolicyInsuStateUtil;
import com.tennetcn.component.webapi.BaseApi;
import com.tennetcn.component.webapi.ResponseModel;
import com.xiaoleilu.hutool.log.dialect.log4j2.Log4j2LogFactory;

@Controller
@RequestMapping(value = "/api/v1/listPolicyAres/",method = {RequestMethod.GET,RequestMethod.POST},produces="application/json;charset=utf-8")
@Api(value="/api/v1/listPolicyAres/")
@CrossOrigin(origins = "*",maxAge = 3600)
public class ListPolicyAresApi extends BaseApi{

	private IFanhuaAccidentService fanhuaAccidentService;
	private IFanhuaCarService fanhuaCarService;
	private IFanhuaCarBusinessRiskKindsService fanhuaCarBusinessRiskKindsService;
	private IZhonganCarService zhonganCarService;
	private ICustomerService customerService;
	
	public ICustomerService getCustomerService() {
		return customerService;
	}
	@Resource
	public void setCustomerService(ICustomerService customerService) {
		this.customerService = customerService;
	}
	public IFanhuaAccidentService getAccidentInsuranceService() {
		return fanhuaAccidentService;
	}
	@Resource
	public void setAccidentInsuranceService(IFanhuaAccidentService fanhuaAccidentService) {
		this.fanhuaAccidentService = fanhuaAccidentService;
	}
	public IFanhuaCarService getFanhuaCarService() {
		return fanhuaCarService;
	}
	@Resource
	public void setFanhuaCarService(IFanhuaCarService fanhuaCarService) {
		this.fanhuaCarService = fanhuaCarService;
	}
	public IFanhuaCarBusinessRiskKindsService getBusinessRiskKindsService() {
		return fanhuaCarBusinessRiskKindsService;
	}
	@Resource
	public void setBusinessRiskKindsService(IFanhuaCarBusinessRiskKindsService fanhuaCarBusinessRiskKindsService) {
		this.fanhuaCarBusinessRiskKindsService = fanhuaCarBusinessRiskKindsService;
	}
	
	private IListPolicyService listPolicyService;
	public IListPolicyService getListPolicyService() {
		return listPolicyService;
	}
	@Resource
	public void setListPolicyService(IListPolicyService listPolicyService) {
		this.listPolicyService = listPolicyService;
	}
	public IZhonganCarService getZhonganCarService() {
		return zhonganCarService;
	}
	@Resource
	public void setZhonganCarService(IZhonganCarService zhonganCarService) {
		this.zhonganCarService = zhonganCarService;
	}

	private final String policyListApi="policyList";
	private final String policyDetailApi="policyDetail";
	private final String carPolicyDetailApi="carPolicyDetail";
	
	@ResponseBody
	@RequestMapping(value = policyListApi)
	@ApiOperation(notes= policyListApi,httpMethod = "POST" ,value = "保单展示(车险/非车险)")
	public String policyList(@ApiParam("车险/非车险") @RequestParam(value="isCar",required=true)String isCar,@ApiParam("保单状态(待生效/承保中/已失效/承保失败)") @RequestParam(value="insuState",required=true)String insuState){

		ResponseModel rm = new ResponseModel();
		String custId=getLoginId();
		Log4j2LogFactory.get().error("custId++++++++"+custId);
		List<ListPolicy> policys = new ArrayList<ListPolicy>();
		
		// 车险筛选
		if ("carInsu".equals(isCar)) {
			System.err.println("进入车险");
			
			List<ListPolicy> carPolicys = listPolicyService.queryAllCarPolicy(custId);
			for (ListPolicy carPolicy : carPolicys) {
				PolicyInsuStateUtil.changeState(carPolicy);
			}

			if ("all".equals(insuState)) {
				for (ListPolicy listPolicy : carPolicys) {
					policys.add(listPolicy);
				}
			} 
			
			else if ("ready".equals(insuState)) {
				for (ListPolicy listPolicy : carPolicys) {
					if (listPolicy.getInsuState().equals("ready")) {
						policys.add(listPolicy);
					}
				}
			} 
			
			else if ("yes".equals(insuState)) {
				for (ListPolicy listPolicy : carPolicys) {
					if (listPolicy.getInsuState().equals("yes")) {
						policys.add(listPolicy);
					}
					System.out.println("承保中 的车险：" + policys.size());
				}
			} 
			
			else if ("no".equals(insuState)) {
				for (ListPolicy listPolicy : carPolicys) {
					if (listPolicy.getInsuState().equals("no")) {
						policys.add(listPolicy);
					}
				}
			}
		}
		
		// 非车险筛选
		if ("notCarInsu".equals(isCar)) {
			System.err.println("进入非车险");
			List<ListPolicy> accidentPolicys = listPolicyService.queryAllAccidentPolicy(custId);

			for (ListPolicy accidentPolicy : accidentPolicys) {
				PolicyInsuStateUtil.changeState(accidentPolicy);
			}

			if ("all".equals(insuState)) {
				for (ListPolicy listPolicy : accidentPolicys) {
					policys.add(listPolicy);
				}
			} 
			
			else if ("ready".equals(insuState)) {
				for (ListPolicy listPolicy : accidentPolicys) {
					if (listPolicy.getInsuState().equals("ready")) {
						policys.add(listPolicy);
					}
				}
			} 
			
			else if ("yes".equals(insuState)) {
				for (ListPolicy listPolicy : accidentPolicys) {
					if (listPolicy.getInsuState().equals("yes")) {
						policys.add(listPolicy);
					}
				}
			} 
			
			else if ("no".equals(insuState)) {
				for (ListPolicy listPolicy : accidentPolicys) {
					if (listPolicy.getInsuState().equals("no")) {
						policys.add(listPolicy);
					}
				}
			} 
			
			else if ("fail".equals(insuState)) {
				for (ListPolicy listPolicy : accidentPolicys) {
					if (listPolicy.getInsuState().equals("fail")) {
						policys.add(listPolicy);
						System.err.println("asdsadsa" + listPolicy.getStartDate());
					}
				}
			}
		} 	
		
		List<String> policysParams = new ArrayList<String>();
		Field[] policysFields = ListPolicy.class.getDeclaredFields();
		for(Field field : policysFields){
			policysParams.add(field.getName());
		} 

		rm.setMessage("查询数据如下");
		rm.putList("policys", policys, policysParams);
		return rm.toJson();
	}
	
	@ResponseBody
	@RequestMapping(value = policyDetailApi)
	@ApiOperation(notes= policyDetailApi,httpMethod = "POST" ,value = "进入非车险详情")
	public String policyDetail(@ApiParam("泛华/众安") @RequestParam(value="signs",required=true)String signs,@ApiParam("保单号") @RequestParam(value="oid",required=true)String oid){
		
		ResponseModel rm = new ResponseModel();
		
		String custId=getLoginId();
		
		ListPolicy listPolicy = new ListPolicy();
		if(signs.equals("fanhuaAccident")){
			if(StringUtils.isEmpty(oid)){
				rm.setMessage("订单号为空");
			}
			listPolicy = listPolicyService.queryFanhuaAccident(custId,oid);
			Log4j2LogFactory.get().error("listPolicy**"+listPolicy);
			if(StringUtils.isEmpty(listPolicy)){
				rm.setMessage("没有查询到数据");
			}
		}
		if (signs.equals("zhonganAccident")){
			if(StringUtils.isEmpty(oid)){
				rm.setMessage("保单号为空");
			}
			listPolicy = listPolicyService.queryZhonganAccident(custId,oid);
			if(StringUtils.isEmpty(listPolicy)){
				rm.setMessage("没有查询到数据");
			}
		}
		
		rm.put("listPolicy", listPolicy);
		
		return rm.toJson();
	}
	
	@ResponseBody
	@RequestMapping(value = carPolicyDetailApi)
	@ApiOperation(notes= carPolicyDetailApi,httpMethod = "POST" ,value = "车险详情")
	public String carPolicyDetail(@ApiParam("泛华/众安") @RequestParam(value="signs",required=true)String signs,@ApiParam("任务号/订单号") @RequestParam(value="oid",required=true)String oid){
		
		ResponseModel rm = new ResponseModel();
		ListPolicy listPolicy = new ListPolicy();
		
		if("fanhuaCar".equals(signs)){
			if(StringUtils.isEmpty(oid)){
				rm.setMessage("保单号为空");
			}
			listPolicy = listPolicyService.queryFanhuaCarDetail(oid);
			if(StringUtils.isEmpty(listPolicy)){
				rm.setMessage("保单号错误");
			}
			FanhuaCar fanhuaCar = fanhuaCarService.queryFanhuaCar(oid);
			if(StringUtils.isEmpty(fanhuaCar)){
				rm.setMessage("保单号为空");
			}
			System.err.println("listPolicy.getFanhuaCar().getId()++"+fanhuaCar.getId());
			
			List<FanhuaCarBusinessRiskKinds> fanhuaCarBusinessRiskKinds = fanhuaCarBusinessRiskKindsService.queryModelByOid(fanhuaCar.getId());
			if(StringUtils.isEmpty(fanhuaCarBusinessRiskKinds)){
				rm.setMessage("没有查询到数据");
			}
			rm.put("fanhuaCarBusinessRiskKinds",fanhuaCarBusinessRiskKinds);
		}
		if("zhonganCar".equals(signs)){
			if(StringUtils.isEmpty(oid)){
				rm.setMessage("保单号错误");
			}
			listPolicy = listPolicyService.queryZhonganCarDetail(oid);
			if(StringUtils.isEmpty(listPolicy)){
				rm.setMessage("保单号错误");
			}
			ZhonganCar zhongancar = zhonganCarService.queryModelByPolicyNo(listPolicy.getOid());
			if(StringUtils.isEmpty(zhongancar)){
				rm.setMessage("保单号错误");
			}
			String vehiclePolicyOrderNo = zhongancar.getVehiclePolicyOrderNo();
			System.err.println("vehiclePolicyOrderNo"+vehiclePolicyOrderNo);

			if(!StringUtils.isEmpty(vehiclePolicyOrderNo)){
				ZhonganCar zhonganCarMain = zhonganCarService.queryModelByVehiclePolicyOrderNo(vehiclePolicyOrderNo,"2");
				rm.put("zhonganCarMain",zhonganCarMain);
			}else {
				ZhonganCar zhonganCarElse = new ZhonganCar();
				rm.put("zhonganCarElse",zhonganCarElse);
			}
		}	
		
		rm.put("listPolicy",listPolicy);
		return rm.toJson();
	}
}
